package com.apps.saspect.impl;


import com.apps.saspect.RunTimes;
import com.apps.saspect.ScriptEvent;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

@Slf4j
@Aspect
@Component
@Configuration
public class RunTimesAspect {

    @Pointcut("@annotation(com.apps.saspect.RunTimes)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object doAround(ProceedingJoinPoint point) throws Throwable {
        long beginTime=System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        RunTimes runTimes=method.getAnnotation(RunTimes.class);
        Object result=point.proceed();
        log.info("方法:"+method.getName()+"执行耗时"+ (runTimes.unit() == TimeUnit.SECONDS ?((System.currentTimeMillis()-beginTime)/1000)+"秒":(System.currentTimeMillis()-beginTime)+"毫秒"));
        return result;
    }
}
